//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package common;

public class ChecksumEncoder {
  private boolean enabled = true;
  private int checksum = 0;
  private int initialChecksum = 0;

  public ChecksumEncoder() {
  }

  public boolean isChecksumOnlyMode() {
    return true;
  }

  public void enableCheckSum(boolean action) {
    if(this.enabled) {
      if(!action) {
        this.initialChecksum = this.checksum;
      }
    } else if(action) {
      this.checksum = this.initialChecksum;
    }

    this.enabled = action;
  }

  private int ror31(int val) {
    int leftBit = (val & -2147483648) != 0?1:0;
    return (val << 1) + leftBit;
  }

  public void writeString(String s) {
    int curcksum = this.ror31(this.checksum);
    if(s != null) {
      curcksum += s.length() + 28;
    } else {
      curcksum += 27;
    }

    this.checksum = curcksum;
  }

  public void writeStringReference(String sr) {
    this.checksum = this.ror31(this.checksum) + sr.length() + 38;
  }

  public void writeBoolean(boolean boolval) {
    int curcksum = !boolval?7:13;
    this.checksum = curcksum + this.ror31(this.checksum);
  }

  public void writeInt(int intVal) {
    this.checksum = this.ror31(this.checksum) + intVal + 9;
  }

  public void writeByte(byte byteVal) {
    this.checksum = this.ror31(this.checksum) + (byteVal & 255) + 11;
  }

  public void writeShort(int shortVal) {
    this.checksum = this.ror31(this.checksum) + (shortVal & '\uffff') + 19;
  }

  public void writeBytes(byte[] bytesVal) {
    int curcksum = this.ror31(this.checksum);
    if(bytesVal != null) {
      curcksum += bytesVal.length + 28;
    } else {
      curcksum += 27;
    }

    this.checksum = curcksum;
  }

  public void writeUnknown(int unkVal) {
    this.checksum = this.ror31(this.checksum) + unkVal + 33;
  }
}
